package org.itboys.core.service.mysql.web;

import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.core.common.SundayCommonConstants;
import org.itboys.core.dao.coupon.SundayCouponRecordMapper;
import org.itboys.core.entity.mongo.SundayMember;
import org.itboys.core.entity.mysql.coupon.SundayCoupon;
import org.itboys.core.entity.mysql.coupon.SundayCouponExample;
import org.itboys.core.entity.mysql.coupon.record.SundayMemberCoupon;
import org.itboys.core.entity.mysql.coupon.record.SundayMemberCouponExample;
import org.itboys.core.service.mongo.web.SundayMemberService;
import org.itboys.core.util.MybatisParamUtil;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author：贾琦磊
 * @Date：2018/12/10
 * @Description：优惠券
 */
@Service
public class SundayCouponRecordService {
    @Autowired
    private SundayCouponRecordMapper couponRecordMapper;
    @Autowired
    private SundayMemberService memberService;

    public PageResult<SundayMemberCoupon> select(MybatisParamUtil paramUtil,Integer used){
        SundayMemberCouponExample example=new SundayMemberCouponExample();
        SundayMemberCouponExample.Criteria criteria = example.createCriteria()
                .andIsDeletedEqualTo(SundayCommonConstants.NO);
        if(used!=null)criteria.andUsedEqualTo(used);
        long total = couponRecordMapper.countByExample(example);
        example.setOrderByClause(paramUtil.getSortName());
        example.setSortOrder(paramUtil.getSortOrder());
        example.setPageSize(paramUtil.getRowSize());
        example.setStartRow(paramUtil.getRowStart());
        List<SundayMemberCoupon> data = couponRecordMapper.selectByExample(example);
        List<Long> memberIds=new ArrayList<>();
        if(ListUtil.isNotNull(data)){
            for (SundayMemberCoupon mc:data
                 ) {
                memberIds.add(mc.getMemberId());
            }
            Map<String,Object> param=new HashMap<>();
            param.put("isDeleted",SundayCommonConstants.NO);
            param.put("id in",memberIds);
            List<SundayMember> members = memberService.list(param);
            for (SundayMemberCoupon mc:data
                    ) {
                for (SundayMember m: members
                     ) {
                    if(mc.getMemberId().equals(m.getId())){
                        mc.setMemberName(m.getRealName());
                        mc.setMobile(m.getMobile());
                        break;
                    }
                }
            }
        }
        return new PageResult<>(data,total);
    }


}
